package com.cjjd.c.utils;


import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ScrollView;

/**
 * UI工具类
 *
 * @author dada
 */
public class UIUtil {

	private static PopupWindow pop = null;

	/**
	 * 显示蒙层
	 *
	 * @param context
	 * @param parent
	 * @param drawableId
	 */
	@SuppressWarnings("deprecation")
	public static void showPopWindow(Context context, View parent, int drawableId) {
		if (drawableId == 0) {
			return;
		}
		if (pop == null) {
			ImageView imageView = null;
			imageView = new ImageView(context);
			imageView.setBackgroundResource(drawableId);
			pop = new PopupWindow(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (pop != null) {
						pop.dismiss();
						pop = null;
					}
				}
			});
		}
		if (!pop.isShowing()) {
			pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

		}
	}

	/**
	 * 设置ViewGroup中的view宽高
	 *
	 * @param view   :视图
	 * @param width  :宽
	 * @param height :高
	 */
	public static void setViewGroupLayoutParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;
		ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width, height);
		view.setLayoutParams(lp);
	}

	/**
	 * 设置RelativeLayout中的view宽高
	 *
	 * @param view   :视图
	 * @param width  :宽
	 * @param height :高
	 */
	public static void setRelativeLayoutParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		LayoutParams lp = (LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}

	public static void setRelativeLayoutParams2(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		AbsListView.LayoutParams lp = (AbsListView.LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}


	/**
	 * 设置FrameLayout中的view宽高
	 *
	 * @param view   :视图
	 * @param width  :宽
	 * @param height :高
	 */
	public static void setFrameLayoutParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}

	/**
	 * 设置AbsListView中的view宽高
	 *
	 * @param view   :视图
	 * @param width  :宽
	 * @param height :高
	 */
	public static void setAbsListViewParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		AbsListView.LayoutParams lp = (AbsListView.LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}

	/**
	 * 设置LinearLayout中的view宽高
	 *
	 * @param view   :视图
	 * @param width  :宽
	 * @param height :高
	 */
	public static void setLinearLayoutParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}

	public static void setScrollLayoutParams(View view, int width, int height) {
		if (width == 0 || height == 0) return;

		ScrollView.LayoutParams lp = (ScrollView.LayoutParams) view.getLayoutParams();
		lp.width = width;
		lp.height = height;
		view.setLayoutParams(lp);
	}

	
	
	/**
	 * 创建ImageView（心情）(width=150dip;height=150dip)
	 *
	 * @param context
	 * @return
	 */
	public static ImageView createMoodContent(Context context) {
		int width = ScreenUtil.dip2px(context, 180);
		int height = ScreenUtil.dip2px(context, 180);
		ImageView imageView = createContentImageView(context);
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
		params.gravity = Gravity.LEFT;
		params.width = width;
		params.height = height;
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setLayoutParams(params);
		return imageView;
	}

	/**
	 * 创建ImageView（帖子）
	 *
	 * @param context
	 * @return
	 */
	public static ImageView createContentImageView(Context context) {
		int imagePxMarginTopBottom = ScreenUtil.dip2px(context, 5);
		int imagePxMarginLeftRight = ScreenUtil.dip2px(context, 6);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER;
		params.leftMargin = imagePxMarginLeftRight;
		params.rightMargin = imagePxMarginLeftRight;
		params.topMargin = imagePxMarginTopBottom;
		params.bottomMargin = imagePxMarginTopBottom;
		ImageView iv = new ImageView(context);
		iv.setLayoutParams(params);
		return iv;
	}

	/**
	 * 设置html格式的颜色值
	 *
	 * @param text  需要设置的文本
	 * @param color 需要设置的颜色：#ffffff
	 * @return
	 */
	public static String setHtmlColor(String text, String color) {

		return "<font color=\"" + color + "\">" + text + "</font>";
	}

}
